function countingSort(arr) {
  let max = Math.max(...arr);
  let count = new Array(max + 1).fill(0);
  let output = new Array(arr.length).fill(0);

  for (let i = 0; i < arr.length; i++) {
    count[arr[i]]++;
  }

  for (let i = 1; i <= max; i++) {
    count[i] += count[i - 1];
  }

  for (let i = arr.length - 1; i >= 0; i--) {
    output[count[arr[i]] - 1] = arr[i];
    count[arr[i]]--;
  }

  return output;

}

function test() {
    let arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
    console.log("before sort:", arr);
    let output = countingSort(arr);
    console.log("after sort:", output);
}

test();